package com.zw.pdm.module.designtask.service;

import com.zw.pdm.module.designtask.mapper.DsnIssueMapper;
import com.zw.pdm.module.designtask.model.DsnProcessModel;
import com.zw.pdm.module.report.model.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Description: 设计状态确认
 * @Author: China.wsg
 * @Date: 2020/04/06 15:15
 */
@Service
@SuppressWarnings("all")
public class DsnIssueService {

    @Autowired
    private DsnIssueMapper dsnIssueMapper;
    @Value("${saleUrl}")
    private String saleUrl;

    /**
     * 设计项目列表（数据）
     *
     * @param dsnProcessModel
     * @return
     */
    public List<DsnProcessModel> findProjectIssueByQuery(DsnProcessModel dsnProcessModel) {

        return dsnIssueMapper.findProjectIssueByQuery(dsnProcessModel);
    }

    /**
     * 图纸下发确认状态修改
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean editIssueStatus(String proCode, Integer dsnIssueStatus) {

        boolean boolFlg = updateIssueStatus(saleUrl + "/projectScheduling/updateIssueStatus", proCode, dsnIssueStatus);
        if (!boolFlg) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

    /**
     * 跨域修改 项目状态
     *
     * @param saleUrl   接口地址
     * @param proCode   项目编号
     * @param proStatus 项目状态
     * @return
     */
    private boolean updateIssueStatus(String saleUrl, String proCode, Integer dsnIssueStatus) {
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>();
        bodyMap.add("proCode", proCode.toString());
        bodyMap.add("dsnIssueStatus", dsnIssueStatus.toString());
        return restTemplate.postForObject(saleUrl, bodyMap, Boolean.class);
    }

    /**
     * 项目信息
     *
     * @return
     */
    public List<Project> findProjectByProCode(String proCode) {

        return dsnIssueMapper.findProjectByProCode(proCode);
    }

}
